{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "619e93e9",
   "metadata": {},
   "source": [
    "# IF"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "300616ea",
   "metadata": {},
   "source": [
    "## 条件测试\n",
    "IF语句的核心条件是一个值为True或者False的表达式，这种表达式成为条件测试。\n",
    "Python根据条件测试的值来决定是否执行If语句中的代码，如果值为True就执行，反之则不执行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "512f4af1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "## 检查值是否相等\n",
    "var = 'a'\n",
    "print( var.upper =='a'.upper)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9875c57c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "## IF语句\n",
    "var = 'a'\n",
    "if var == 'a':\n",
    "    print(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f7930a28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "## IF-ELSE语句\n",
    "var = 'a'\n",
    "if var == 'b':\n",
    "    print(True)\n",
    "else:\n",
    "    print(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6434e684",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "A\n",
      "A\n"
     ]
    }
   ],
   "source": [
    "## IF-ELIF-ElSE语句\n",
    "var = 'variable'\n",
    "\n",
    "### 单个ELIF\n",
    "if var == 'v':\n",
    "    print(True)\n",
    "elif len(var) > 1:\n",
    "    print(len(var))\n",
    "else:\n",
    "    print(False)\n",
    "\n",
    "    \n",
    "### 多个ELIF\n",
    "if var == 'v':\n",
    "    print(True)\n",
    "elif len(var) < 1:\n",
    "    print(len(var))\n",
    "    ### 查找 var变量中是否包含‘a’\n",
    "elif var.find('a'):\n",
    "    print('A')\n",
    "else:\n",
    "    print(False)\n",
    "\n",
    "### 省略ELSE语句\n",
    "if var == 'v':\n",
    "    print(True)\n",
    "elif len(var) < 1:\n",
    "    print(len(var))\n",
    "    ### 查找 var变量中是否包含‘a’\n",
    "elif var.find('a'):\n",
    "    print('A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "55e24ccc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "## 使用IF处理列表\n",
    "list = ['a', 'b', 'c', 'd', 'e']\n",
    "\n",
    "if len(list) != 0:\n",
    "    for var in list:\n",
    "        if var == 'a':\n",
    "            print(list[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
